
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
    public:
    int pathSum(TreeNode* root) {
        if(root->left)
        return cal(root, 0);
    }
    
    int cal(TreeNode* u, int height)
    {
        if(u->left==nullptr&&u->right==nullptr) return height*u->val; //叶子节点
        int res=0;
        if(u->left) res+=cal(u->left, height+1);
        if(u->right) res+=cal(u->right, height+1);
        return res;
    }
};